# -*- coding: utf-8 -*-

import scrapy
from scrapy.http.request import Request

from scrapyImg.items import ScrapyimgItem


class A58picSpider(scrapy.Spider):
    name = '58pic'
    totalPages = -1
    allowed_domains = ['58pic.com']
    #start_urls = ['http://58pic.com/']
    
    def start_requests(self):
        #首次访问地址
        url = "http://www.58pic.com/piccate/9-187-824.html"
        yield Request(url)
        
    def parse(self, response):
        item = ScrapyimgItem()
        item["name"] = response.xpath("//a[@class='thumb-box']/img/@alt").extract()
        #不能如此简单处理，因为网站对部分图片做了lazy，src的值不准确
        #item["imgUrl"] = response.xpath("//a[@class='thumb-box']/img/@src").extract()
        #改为获取到图片详情页地址
        item["imgDetailUrl"] = response.xpath("//a[@class='thumb-box']/@href").extract()
        
        #获取分页总页数
        if self.totalPages ==-1:
            self.totalPages = response.xpath("//div[@class='qt-pagination']/a[@rel='external nofollow'][1]/text()").extract()[0]
            self.totalPages = int(self.totalPages)
            print("==》总页数：",self.totalPages)
            
        yield item
        
        if self.totalPages > 0:
            for i in range(2,int(self.totalPages)+1):
                url = "http://www.58pic.com/piccate/9-187-824-"+str(i)+".html"
                yield Request(url)
